<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"littlefxc.github.io","root":"/blog/","images":"/blog/images","scheme":"Mist","version":"8.2.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/blog/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta name="description" content="1 准备数据 建立索引 shop（名字随意）   123POST &#x2F;shop&#x2F;_mapping&amp;#123; &quot;properties&quot;: &amp;#123; &quot;id&quot;: &amp;#123; &quot;type&quot;: &quot;long&quot; &amp;#125;, &quot;age&quot;: &amp;#123; &quot;type&quot;: &quot;i">
<meta property="og:type" content="article">
<meta property="og:title" content="ES 之 DSL 搜索">
<meta property="og:url" content="http://littlefxc.github.io/2021/01/12/ES-%E4%B9%8B-DSL-%E6%90%9C%E7%B4%A2/index.html">
<meta property="og:site_name" content="一年春又来">
<meta property="og:description" content="1 准备数据 建立索引 shop（名字随意）   123POST &#x2F;shop&#x2F;_mapping&amp;#123; &quot;properties&quot;: &amp;#123; &quot;id&quot;: &amp;#123; &quot;type&quot;: &quot;long&quot; &amp;#125;, &quot;age&quot;: &amp;#123; &quot;type&quot;: &quot;i">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/match_all_12_1.jpg">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/match_all_12_2.jpg">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/5df73ae408b52e4b14850283.jpg">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/5df73af9088d880314890314.jpg">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/es-12_3.jpg">
<meta property="article:published_time" content="2021-01-12T12:56:00.000Z">
<meta property="article:modified_time" content="2021-03-25T13:15:48.944Z">
<meta property="article:author" content="一年春又来">
<meta property="article:tag" content="es">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/littlefxc/oss/raw/master/images/match_all_12_1.jpg">


<link rel="canonical" href="http://littlefxc.github.io/2021/01/12/ES-%E4%B9%8B-DSL-%E6%90%9C%E7%B4%A2/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>
<title>ES 之 DSL 搜索 | 一年春又来</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/blog/atom.xml" title="一年春又来" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/blog/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">一年春又来</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/blog/" rel="section"><i class="home                          //首页 fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-archives"><a href="/blog/archives/" rel="section"><i class="archive          //归档 fa-fw"></i>归档</a></li>
        <li class="menu-item menu-item-categories"><a href="/blog/categories/" rel="section"><i class="th           //分类 fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-tags"><a href="/blog/tags/" rel="section"><i class="tags                     //标签 fa-fw"></i>标签</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-%E5%87%86%E5%A4%87%E6%95%B0%E6%8D%AE"><span class="nav-text">1 准备数据</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-%E4%BD%BF%E7%94%A8-QueryString-%E6%9F%A5%E8%AF%A2"><span class="nav-text">2 使用 QueryString 查询</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3-DSL"><span class="nav-text">3 DSL</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#3-1-match-%E6%9F%A5%E8%AF%A2"><span class="nav-text">3.1 match - 查询</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-2-exists-%E5%88%A4%E6%96%AD%E6%9F%90%E4%B8%AA%E5%AD%97%E6%AE%B5%E6%98%AF%E5%90%A6%E5%AD%98%E5%9C%A8"><span class="nav-text">3.2 exists - 判断某个字段是否存在</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-3-match-all-%E6%9F%A5%E8%AF%A2%E6%89%80%E6%9C%89%E4%B8%8E%E5%88%86%E9%A1%B5"><span class="nav-text">3.3 match_all - 查询所有与分页</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-1-%E5%9C%A8%E7%B4%A2%E5%BC%95%E4%B8%AD%E6%9F%A5%E8%AF%A2%E6%89%80%E6%9C%89%E7%9A%84%E6%96%87%E6%A1%A3"><span class="nav-text">3.3.1 在索引中查询所有的文档</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-3-2-%E5%88%86%E9%A1%B5%E6%9F%A5%E8%AF%A2"><span class="nav-text">3.3.2 分页查询</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-4-term-%E7%B2%BE%E7%A1%AE%E6%9F%A5%E8%AF%A2"><span class="nav-text">3.4 term - 精确查询</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4-1-term%E7%B2%BE%E7%A1%AE%E6%90%9C%E7%B4%A2%E4%B8%8Ematch%E5%88%86%E8%AF%8D%E6%90%9C%E7%B4%A2"><span class="nav-text">3.4.1 term精确搜索与match分词搜索</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-4-2-terms-%E5%A4%9A%E4%B8%AA%E8%AF%8D%E8%AF%AD%E5%8C%B9%E9%85%8D%E6%A3%80%E7%B4%A2"><span class="nav-text">3.4.2 terms 多个词语匹配检索</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-5-match-phrase"><span class="nav-text">3.5 match_phrase</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-6-match%EF%BC%88operator%EF%BC%89-ids"><span class="nav-text">3.6 match（operator）&#x2F;ids</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-6-1-match-%E6%89%A9%E5%B1%95"><span class="nav-text">3.6.1 match 扩展</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-6-2-%E6%A0%B9%E6%8D%AE%E6%96%87%E6%A1%A3%E4%B8%BB%E9%94%AEids%E6%90%9C%E7%B4%A2"><span class="nav-text">3.6.2 根据文档主键ids搜索</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-7-multi-match-boost"><span class="nav-text">3.7 multi_match&#x2F;boost</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#3-7-1-multi-match-%E5%A4%9A%E5%AD%97%E6%AE%B5%E6%9F%A5%E8%AF%A2"><span class="nav-text">3.7.1 multi_match - 多字段查询</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-7-2-boost-%E6%9D%83%E9%87%8D"><span class="nav-text">3.7.2 boost - 权重</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-8-bool-%E5%B8%83%E5%B0%94%E6%9F%A5%E8%AF%A2"><span class="nav-text">3.8 bool - 布尔查询</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-9-post-filter-%E8%BF%87%E6%BB%A4%E5%99%A8"><span class="nav-text">3.9 post_filter - 过滤器</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-10-sort-%E6%8E%92%E5%BA%8F"><span class="nav-text">3.10 sort - 排序</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-11-highlight-%E9%AB%98%E4%BA%AE%E6%98%BE%E7%A4%BA"><span class="nav-text">3.11 highlight - 高亮显示</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-12-prefix-%E5%89%8D%E7%BC%80"><span class="nav-text">3.12 prefix - 前缀</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-13-fuzzy-%E6%A8%A1%E7%B3%8A%E6%90%9C%E7%B4%A2"><span class="nav-text">3.13 fuzzy - 模糊搜索</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#3-14-wildcard-%E5%8D%A0%E4%BD%8D%E7%AC%A6%E6%9F%A5%E8%AF%A2"><span class="nav-text">3.14 wildcard - 占位符查询</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">一年春又来</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">184</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">35</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">115</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/01/12/ES-%E4%B9%8B-DSL-%E6%90%9C%E7%B4%A2/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          ES 之 DSL 搜索
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-01-12 20:56:00" itemprop="dateCreated datePublished" datetime="2021-01-12T20:56:00+08:00">2021-01-12</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:48" itemprop="dateModified" datetime="2021-03-25T21:15:48+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/blog/categories/ELK/" itemprop="url" rel="index"><span itemprop="name">ELK</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="1-准备数据"><a href="#1-准备数据" class="headerlink" title="1 准备数据"></a>1 准备数据</h1><ul>
<li><p>建立索引 shop（名字随意）</p>
  <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">POST /shop/_mapping</span><br><span class="line">&#123; <span class="string">&quot;properties&quot;</span>: &#123; <span class="string">&quot;id&quot;</span>: &#123; <span class="string">&quot;type&quot;</span>: <span class="string">&quot;long&quot;</span> &#125;, <span class="string">&quot;age&quot;</span>: &#123; <span class="string">&quot;type&quot;</span>: <span class="string">&quot;integer&quot;</span> &#125;, <span class="string">&quot;username&quot;</span>: &#123; <span class="string">&quot;type&quot;</span>: <span class="string">&quot;keyword&quot;</span> &#125;, <span class="string">&quot;nickname&quot;</span>: &#123; <span class="string">&quot;type&quot;</span>: <span class="string">&quot;text&quot;</span>, <span class="string">&quot;analyzer&quot;</span>: <span class="string">&quot;ik_max_word&quot;</span> &#125;, <span class="string">&quot;money&quot;</span>: &#123; <span class="string">&quot;type&quot;</span>: <span class="string">&quot;float&quot;</span> &#125;, <span class="string">&quot;desc&quot;</span>: &#123; <span class="string">&quot;type&quot;</span>: <span class="string">&quot;text&quot;</span>, <span class="string">&quot;analyzer&quot;</span>: <span class="string">&quot;ik_max_word&quot;</span> &#125;, <span class="string">&quot;sex&quot;</span>: &#123; <span class="string">&quot;type&quot;</span>: <span class="string">&quot;byte&quot;</span> &#125;, <span class="string">&quot;birthday&quot;</span>: &#123; <span class="string">&quot;type&quot;</span>: <span class="string">&quot;date&quot;</span> &#125;, <span class="string">&quot;face&quot;</span>: &#123; <span class="string">&quot;type&quot;</span>: <span class="string">&quot;text&quot;</span>, <span class="string">&quot;index&quot;</span>: <span class="literal">false</span> &#125; &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>录入数据</p>
  <figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br></pre></td><td class="code"><pre><span class="line">POST /shop/_doc/1001</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;id&quot;</span>: 1001,</span><br><span class="line">    <span class="string">&quot;age&quot;</span>: 18,</span><br><span class="line">    <span class="string">&quot;username&quot;</span>: <span class="string">&quot;imoocAmazing&quot;</span>,</span><br><span class="line">    <span class="string">&quot;nickname&quot;</span>: <span class="string">&quot;慕课网&quot;</span>,</span><br><span class="line">    <span class="string">&quot;money&quot;</span>: 88.8,</span><br><span class="line">    <span class="string">&quot;desc&quot;</span>: <span class="string">&quot;我在慕课网学习java和前端，学习到了很多知识&quot;</span>,</span><br><span class="line">    <span class="string">&quot;sex&quot;</span>: 0,</span><br><span class="line">    <span class="string">&quot;birthday&quot;</span>: <span class="string">&quot;1992-12-24&quot;</span>,</span><br><span class="line">    <span class="string">&quot;face&quot;</span>: <span class="string">&quot;https://www.imooc.com/static/img/index/logo.png&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">POST /shop/_doc/1002</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;id&quot;</span>: 1002,</span><br><span class="line">    <span class="string">&quot;age&quot;</span>: 19,</span><br><span class="line">    <span class="string">&quot;username&quot;</span>: <span class="string">&quot;justbuy&quot;</span>,</span><br><span class="line">    <span class="string">&quot;nickname&quot;</span>: <span class="string">&quot;周杰棍&quot;</span>,</span><br><span class="line">    <span class="string">&quot;money&quot;</span>: 77.8,</span><br><span class="line">    <span class="string">&quot;desc&quot;</span>: <span class="string">&quot;今天上下班都很堵，车流量很大&quot;</span>,</span><br><span class="line">    <span class="string">&quot;sex&quot;</span>: 1,</span><br><span class="line">    <span class="string">&quot;birthday&quot;</span>: <span class="string">&quot;1993-01-24&quot;</span>,</span><br><span class="line">    <span class="string">&quot;face&quot;</span>: <span class="string">&quot;https://www.imooc.com/static/img/index/logo.png&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">POST /shop/_doc/1003</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;id&quot;</span>: 1003,</span><br><span class="line">    <span class="string">&quot;age&quot;</span>: 20,</span><br><span class="line">    <span class="string">&quot;username&quot;</span>: <span class="string">&quot;bigFace&quot;</span>,</span><br><span class="line">    <span class="string">&quot;nickname&quot;</span>: <span class="string">&quot;飞翔的巨鹰&quot;</span>,</span><br><span class="line">    <span class="string">&quot;money&quot;</span>: 66.8,</span><br><span class="line">    <span class="string">&quot;desc&quot;</span>: <span class="string">&quot;慕课网团队和导游坐飞机去海外旅游，去了新马泰和欧洲&quot;</span>,</span><br><span class="line">    <span class="string">&quot;sex&quot;</span>: 1,</span><br><span class="line">    <span class="string">&quot;birthday&quot;</span>: <span class="string">&quot;1996-01-14&quot;</span>,</span><br><span class="line">    <span class="string">&quot;face&quot;</span>: <span class="string">&quot;https://www.imooc.com/static/img/index/logo.png&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">POST /shop/_doc/1004</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;id&quot;</span>: 1004,</span><br><span class="line">    <span class="string">&quot;age&quot;</span>: 22,</span><br><span class="line">    <span class="string">&quot;username&quot;</span>: <span class="string">&quot;flyfish&quot;</span>,</span><br><span class="line">    <span class="string">&quot;nickname&quot;</span>: <span class="string">&quot;水中鱼&quot;</span>,</span><br><span class="line">    <span class="string">&quot;money&quot;</span>: 55.8,</span><br><span class="line">    <span class="string">&quot;desc&quot;</span>: <span class="string">&quot;昨天在学校的池塘里，看到有很多鱼在游泳，然后就去慕课网上课了&quot;</span>,</span><br><span class="line">    <span class="string">&quot;sex&quot;</span>: 0,</span><br><span class="line">    <span class="string">&quot;birthday&quot;</span>: <span class="string">&quot;1988-02-14&quot;</span>,</span><br><span class="line">    <span class="string">&quot;face&quot;</span>: <span class="string">&quot;https://www.imooc.com/static/img/index/logo.png&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">POST /shop/_doc/1005</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;id&quot;</span>: 1005,</span><br><span class="line">    <span class="string">&quot;age&quot;</span>: 25,</span><br><span class="line">    <span class="string">&quot;username&quot;</span>: <span class="string">&quot;gotoplay&quot;</span>,</span><br><span class="line">    <span class="string">&quot;nickname&quot;</span>: <span class="string">&quot;ps游戏机&quot;</span>,</span><br><span class="line">    <span class="string">&quot;money&quot;</span>: 155.8,</span><br><span class="line">    <span class="string">&quot;desc&quot;</span>: <span class="string">&quot;今年生日，女友送了我一台play station游戏机，非常好玩，非常不错&quot;</span>,</span><br><span class="line">    <span class="string">&quot;sex&quot;</span>: 1,</span><br><span class="line">    <span class="string">&quot;birthday&quot;</span>: <span class="string">&quot;1989-03-14&quot;</span>,</span><br><span class="line">    <span class="string">&quot;face&quot;</span>: <span class="string">&quot;https://www.imooc.com/static/img/index/logo.png&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">POST /shop/_doc/1006</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;id&quot;</span>: 1006,</span><br><span class="line">    <span class="string">&quot;age&quot;</span>: 19,</span><br><span class="line">    <span class="string">&quot;username&quot;</span>: <span class="string">&quot;missimooc&quot;</span>,</span><br><span class="line">    <span class="string">&quot;nickname&quot;</span>: <span class="string">&quot;我叫小慕&quot;</span>,</span><br><span class="line">    <span class="string">&quot;money&quot;</span>: 156.8,</span><br><span class="line">    <span class="string">&quot;desc&quot;</span>: <span class="string">&quot;我叫凌云慕，今年20岁，是一名律师，我在琦䯲星球做演讲&quot;</span>,</span><br><span class="line">    <span class="string">&quot;sex&quot;</span>: 1,</span><br><span class="line">    <span class="string">&quot;birthday&quot;</span>: <span class="string">&quot;1993-04-14&quot;</span>,</span><br><span class="line">    <span class="string">&quot;face&quot;</span>: <span class="string">&quot;https://www.imooc.com/static/img/index/logo.png&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">POST /shop/_doc/1007</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;id&quot;</span>: 1007,</span><br><span class="line">    <span class="string">&quot;age&quot;</span>: 19,</span><br><span class="line">    <span class="string">&quot;username&quot;</span>: <span class="string">&quot;msgame&quot;</span>,</span><br><span class="line">    <span class="string">&quot;nickname&quot;</span>: <span class="string">&quot;gamexbox&quot;</span>,</span><br><span class="line">    <span class="string">&quot;money&quot;</span>: 1056.8,</span><br><span class="line">    <span class="string">&quot;desc&quot;</span>: <span class="string">&quot;明天去进货，最近微软处理很多游戏机，还要买xbox游戏卡带&quot;</span>,</span><br><span class="line">    <span class="string">&quot;sex&quot;</span>: 1,</span><br><span class="line">    <span class="string">&quot;birthday&quot;</span>: <span class="string">&quot;1985-05-14&quot;</span>,</span><br><span class="line">    <span class="string">&quot;face&quot;</span>: <span class="string">&quot;https://www.imooc.com/static/img/index/logo.png&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">POST /shop/_doc/1008</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;id&quot;</span>: 1008,</span><br><span class="line">    <span class="string">&quot;age&quot;</span>: 19,</span><br><span class="line">    <span class="string">&quot;username&quot;</span>: <span class="string">&quot;muke&quot;</span>,</span><br><span class="line">    <span class="string">&quot;nickname&quot;</span>: <span class="string">&quot;慕学习&quot;</span>,</span><br><span class="line">    <span class="string">&quot;money&quot;</span>: 1056.8,</span><br><span class="line">    <span class="string">&quot;desc&quot;</span>: <span class="string">&quot;大学毕业后，可以到imooc.com进修&quot;</span>,</span><br><span class="line">    <span class="string">&quot;sex&quot;</span>: 1,</span><br><span class="line">    <span class="string">&quot;birthday&quot;</span>: <span class="string">&quot;1995-06-14&quot;</span>,</span><br><span class="line">    <span class="string">&quot;face&quot;</span>: <span class="string">&quot;https://www.imooc.com/static/img/index/logo.png&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">POST /shop/_doc/1009</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;id&quot;</span>: 1009,</span><br><span class="line">    <span class="string">&quot;age&quot;</span>: 22,</span><br><span class="line">    <span class="string">&quot;username&quot;</span>: <span class="string">&quot;shaonian&quot;</span>,</span><br><span class="line">    <span class="string">&quot;nickname&quot;</span>: <span class="string">&quot;骚年轮&quot;</span>,</span><br><span class="line">    <span class="string">&quot;money&quot;</span>: 96.8,</span><br><span class="line">    <span class="string">&quot;desc&quot;</span>: <span class="string">&quot;骚年在大学毕业后，考研究生去了&quot;</span>,</span><br><span class="line">    <span class="string">&quot;sex&quot;</span>: 1,</span><br><span class="line">    <span class="string">&quot;birthday&quot;</span>: <span class="string">&quot;1998-07-14&quot;</span>,</span><br><span class="line">    <span class="string">&quot;face&quot;</span>: <span class="string">&quot;https://www.imooc.com/static/img/index/logo.png&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">POST /shop/_doc/1010</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;id&quot;</span>: 1010,</span><br><span class="line">    <span class="string">&quot;age&quot;</span>: 30,</span><br><span class="line">    <span class="string">&quot;username&quot;</span>: <span class="string">&quot;tata&quot;</span>,</span><br><span class="line">    <span class="string">&quot;nickname&quot;</span>: <span class="string">&quot;隔壁老王&quot;</span>,</span><br><span class="line">    <span class="string">&quot;money&quot;</span>: 100.8,</span><br><span class="line">    <span class="string">&quot;desc&quot;</span>: <span class="string">&quot;隔壁老外去国外出差，带给我很多好吃的&quot;</span>,</span><br><span class="line">    <span class="string">&quot;sex&quot;</span>: 1,</span><br><span class="line">    <span class="string">&quot;birthday&quot;</span>: <span class="string">&quot;1988-07-14&quot;</span>,</span><br><span class="line">    <span class="string">&quot;face&quot;</span>: <span class="string">&quot;https://www.imooc.com/static/img/index/logo.png&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">POST /shop/_doc/1011</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;id&quot;</span>: 1011,</span><br><span class="line">    <span class="string">&quot;age&quot;</span>: 31,</span><br><span class="line">    <span class="string">&quot;username&quot;</span>: <span class="string">&quot;sprder&quot;</span>,</span><br><span class="line">    <span class="string">&quot;nickname&quot;</span>: <span class="string">&quot;皮特帕克&quot;</span>,</span><br><span class="line">    <span class="string">&quot;money&quot;</span>: 180.8,</span><br><span class="line">    <span class="string">&quot;desc&quot;</span>: <span class="string">&quot;它是一个超级英雄&quot;</span>,</span><br><span class="line">    <span class="string">&quot;sex&quot;</span>: 1,</span><br><span class="line">    <span class="string">&quot;birthday&quot;</span>: <span class="string">&quot;1989-08-14&quot;</span>,</span><br><span class="line">    <span class="string">&quot;face&quot;</span>: <span class="string">&quot;https://www.imooc.com/static/img/index/logo.png&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">POST /shop/_doc/1012</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;id&quot;</span>: 1012,</span><br><span class="line">    <span class="string">&quot;age&quot;</span>: 31,</span><br><span class="line">    <span class="string">&quot;username&quot;</span>: <span class="string">&quot;super hero&quot;</span>,</span><br><span class="line">    <span class="string">&quot;nickname&quot;</span>: <span class="string">&quot;super hero&quot;</span>,</span><br><span class="line">    <span class="string">&quot;money&quot;</span>: 188.8,</span><br><span class="line">    <span class="string">&quot;desc&quot;</span>: <span class="string">&quot;BatMan, GreenArrow, SpiderMan, IronMan... are all Super Hero&quot;</span>,</span><br><span class="line">    <span class="string">&quot;sex&quot;</span>: 1,</span><br><span class="line">    <span class="string">&quot;birthday&quot;</span>: <span class="string">&quot;1980-08-14&quot;</span>,</span><br><span class="line">    <span class="string">&quot;face&quot;</span>: <span class="string">&quot;https://www.imooc.com/static/img/index/logo.png&quot;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<h1 id="2-使用-QueryString-查询"><a href="#2-使用-QueryString-查询" class="headerlink" title="2 使用 QueryString 查询"></a>2 使用 QueryString 查询</h1><p>查询[字段]包含[内容]的文档</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">GET /shop/_doc/_search?q=desc:慕课网</span><br><span class="line">GET /shop/_doc/_search?q=nickname:慕&amp;q=age:<span class="number">25</span></span><br></pre></td></tr></table></figure>

<h1 id="3-DSL"><a href="#3-DSL" class="headerlink" title="3 DSL"></a>3 DSL</h1><h2 id="3-1-match-查询"><a href="#3-1-match-查询" class="headerlink" title="3.1 match - 查询"></a>3.1 match - 查询</h2><p>全文检索</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">POST /shop/_search</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;match&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;desc&quot;</span>: <span class="string">&quot;慕课网&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="3-2-exists-判断某个字段是否存在"><a href="#3-2-exists-判断某个字段是否存在" class="headerlink" title="3.2 exists - 判断某个字段是否存在"></a>3.2 exists - 判断某个字段是否存在</h2><p>exists可以理解为SQL中的exists函数，就是判断是否存在该字段。</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">POST /shop/_search</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;exists&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;field&quot;</span>: <span class="string">&quot;desc&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="3-3-match-all-查询所有与分页"><a href="#3-3-match-all-查询所有与分页" class="headerlink" title="3.3 match_all - 查询所有与分页"></a>3.3 match_all - 查询所有与分页</h2><p>可以查询集群所有索引库的信息，包括一些隐藏索性库的信息。</p>
<h3 id="3-3-1-在索引中查询所有的文档"><a href="#3-3-1-在索引中查询所有的文档" class="headerlink" title="3.3.1 在索引中查询所有的文档"></a>3.3.1 在索引中查询所有的文档</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">GET /shop/_doc/_search</span><br></pre></td></tr></table></figure>

<p>或者指定返回属性</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">POST /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;match_all&quot;</span>: &#123;&#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&quot;_source&quot;</span>: [<span class="string">&quot;id&quot;</span>, <span class="string">&quot;nickname&quot;</span>, <span class="string">&quot;age&quot;</span>]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><p>Head 可视化操作</p>
<p>  <img src="https://gitee.com/littlefxc/oss/raw/master/images/match_all_12_1.jpg" alt="https://gitee.com/littlefxc/oss/raw/master/images/match_all_12_1.jpg"></p>
</li>
</ul>
<h3 id="3-3-2-分页查询"><a href="#3-3-2-分页查询" class="headerlink" title="3.3.2 分页查询"></a>3.3.2 分页查询</h3><p>默认查询是只有10条记录，可以通过分页来展示</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">POST /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;match_all&quot;</span>: &#123;&#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&quot;from&quot;</span>: <span class="number">0</span>,</span><br><span class="line">    <span class="string">&quot;size&quot;</span>: <span class="number">10</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">POST /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">	<span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">		<span class="string">&quot;match_all&quot;</span>: &#123;&#125;</span><br><span class="line">	&#125;,</span><br><span class="line">	<span class="string">&quot;_source&quot;</span>: [</span><br><span class="line">		<span class="string">&quot;id&quot;</span>,</span><br><span class="line">		<span class="string">&quot;nickname&quot;</span>,</span><br><span class="line">		<span class="string">&quot;age&quot;</span></span><br><span class="line">	],</span><br><span class="line">	<span class="string">&quot;from&quot;</span>: <span class="number">5</span>,</span><br><span class="line">	<span class="string">&quot;size&quot;</span>: <span class="number">5</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><p>Head 可视化操作</p>
<p>  <img src="https://gitee.com/littlefxc/oss/raw/master/images/match_all_12_2.jpg" alt="https://gitee.com/littlefxc/oss/raw/master/images/match_all_12_2.jpg"></p>
</li>
</ul>
<h2 id="3-4-term-精确查询"><a href="#3-4-term-精确查询" class="headerlink" title="3.4 term - 精确查询"></a>3.4 term - 精确查询</h2><p>term主要用于精确匹配哪些值，比如数字，日期，布尔值或 not_analyzed 的字符串(未经分析的文本数据类型)</p>
<h3 id="3-4-1-term精确搜索与match分词搜索"><a href="#3-4-1-term精确搜索与match分词搜索" class="headerlink" title="3.4.1 term精确搜索与match分词搜索"></a>3.4.1 term精确搜索与match分词搜索</h3><p>搜索的时候会把用户搜索内容，比如“慕课网强大”作为一整个关键词去搜索，而不会对其进行分词后再搜索</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">POST /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;term&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;desc&quot;</span>: <span class="string">&quot;慕课网&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">对比</span><br><span class="line">POST /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;match&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;desc&quot;</span>: <span class="string">&quot;慕课网&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li><p>注：match会对<code>慕课网慕课网</code></p>
<p>  先进行分词（其实就是全文检索），在查询，而term则不会，直接把</p>
<p>  作为一个整的词汇去搜索。</p>
</li>
<li><p>head 可视化操作对比：</p>
<p>  <img src="https://gitee.com/littlefxc/oss/raw/master/images/5df73ae408b52e4b14850283.jpg" alt="https://climg.mukewang.com/5df73ae408b52e4b14850283.jpg"></p>
<p>  <img src="https://gitee.com/littlefxc/oss/raw/master/images/5df73af9088d880314890314.jpg" alt="https://climg.mukewang.com/5df73af9088d880314890314.jpg"></p>
</li>
</ul>
<h3 id="3-4-2-terms-多个词语匹配检索"><a href="#3-4-2-terms-多个词语匹配检索" class="headerlink" title="3.4.2 terms 多个词语匹配检索"></a>3.4.2 terms 多个词语匹配检索</h3><p>相当于是tag标签查询，比如慕课网的一些课程会打上<code>前端</code>/<code>后端</code>/<code>大数据</code>/<code>就业课</code>这样的标签，可以完全匹配做类似标签的查询</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">POST /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;terms&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;desc&quot;</span>: [<span class="string">&quot;慕课网&quot;</span>, <span class="string">&quot;学习&quot;</span>, <span class="string">&quot;骚年&quot;</span>]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="3-5-match-phrase"><a href="#3-5-match-phrase" class="headerlink" title="3.5 match_phrase"></a>3.5 match_phrase</h2><p>match：分词后只要有匹配就返回，match_phrase：分词结果必须在text字段分词中都包含，而且顺序必须相同，而且必须都是连续的。（搜索比较严格）</p>
<p>slop：允许词语间跳过的数量</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">POST /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;match_phrase&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;desc&quot;</span>: &#123;</span><br><span class="line">            	<span class="string">&quot;query&quot;</span>: <span class="string">&quot;大学 毕业 研究生&quot;</span>,</span><br><span class="line">            	<span class="string">&quot;slop&quot;</span>: <span class="number">2</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="3-6-match（operator）-ids"><a href="#3-6-match（operator）-ids" class="headerlink" title="3.6 match（operator）/ids"></a>3.6 match（operator）/ids</h2><h3 id="3-6-1-match-扩展"><a href="#3-6-1-match-扩展" class="headerlink" title="3.6.1 match 扩展"></a>3.6.1 match 扩展</h3><ul>
<li><p>operator</p>
<ul>
<li>or：搜索内容分词后，只要存在一个词语匹配就展示结果</li>
<li>and：搜索内容分词后，都要满足词语匹配</li>
</ul>
  <figure class="highlight"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line">POST     /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;match&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;desc&quot;</span>: <span class="string">&quot;慕课网&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"># 等同于</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;match&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;desc&quot;</span>: &#123;</span><br><span class="line">                <span class="string">&quot;query&quot;</span>: <span class="string">&quot;xbox游戏机&quot;</span>,</span><br><span class="line">                <span class="string">&quot;operator&quot;</span>: <span class="string">&quot;or&quot;</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"># 相当于 select * from shop where desc=&#x27;xbox&#x27; or|and desc=&#x27;游戏机&#x27;</span><br></pre></td></tr></table></figure></li>
<li><p>minimum_should_match: 最低匹配精度，至少有[分词后的词语个数]x百分百，得出一个数据值取整。举个例子：当前属性设置为<code>70</code>，若一个用户查询检索内容分词后有10个词语，那么匹配度按照 10x70%=7，则desc中至少需要有7个词语匹配，就展示；若分词后有8个，则 8x70%=5.6，则desc中至少需要有5个词语匹配，就展示。</p>
<p>  minimum_should_match 也能设置具体的数字，表示个数</p>
  <figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">POST /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;match&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;desc&quot;</span>: &#123;</span><br><span class="line">                <span class="string">&quot;query&quot;</span>: <span class="string">&quot;女友生日送我好玩的xbox游戏机&quot;</span>,</span><br><span class="line">                <span class="string">&quot;minimum_should_match&quot;</span>: <span class="string">&quot;60%&quot;</span></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<h3 id="3-6-2-根据文档主键ids搜索"><a href="#3-6-2-根据文档主键ids搜索" class="headerlink" title="3.6.2 根据文档主键ids搜索"></a>3.6.2 根据文档主键ids搜索</h3><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">GET /shop/_doc/<span class="number">1001</span></span><br></pre></td></tr></table></figure>

<p>查询多个</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">POST /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;ids&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;type&quot;</span>: <span class="string">&quot;_doc&quot;</span>,</span><br><span class="line">            <span class="string">&quot;values&quot;</span>: [<span class="string">&quot;1001&quot;</span>, <span class="string">&quot;1010&quot;</span>, <span class="string">&quot;1008&quot;</span>]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="3-7-multi-match-boost"><a href="#3-7-multi-match-boost" class="headerlink" title="3.7 multi_match/boost"></a>3.7 multi_match/boost</h2><h3 id="3-7-1-multi-match-多字段查询"><a href="#3-7-1-multi-match-多字段查询" class="headerlink" title="3.7.1 multi_match - 多字段查询"></a>3.7.1 multi_match - 多字段查询</h3><p>满足使用match在多个字段中进行查询的需求</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">POST /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;multi_match&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;query&quot;</span>: <span class="string">&quot;皮特帕克慕课网&quot;</span>,</span><br><span class="line">            <span class="string">&quot;fields&quot;</span>: [<span class="string">&quot;desc&quot;</span>, <span class="string">&quot;nickname&quot;</span>]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="3-7-2-boost-权重"><a href="#3-7-2-boost-权重" class="headerlink" title="3.7.2 boost - 权重"></a>3.7.2 boost - 权重</h3><p>权重，为某个字段设置权重，权重越高，文档相关性得分就越高。通畅来说搜索商品名称要比商品简介的权重更高。</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">POST /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;multi_match&quot;</span>: &#123;</span><br><span class="line">                <span class="string">&quot;query&quot;</span>: <span class="string">&quot;皮特帕克慕课网&quot;</span>,</span><br><span class="line">                <span class="string">&quot;fields&quot;</span>: [<span class="string">&quot;desc&quot;</span>, <span class="string">&quot;nickname^10&quot;</span>]</span><br><span class="line"></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><code>nickname^10</code> 代表搜索提升10倍相关性，也就是说用户搜索的时候其实以这个<code>nickname</code>为主，<code>desc</code>为辅，<code>nickname</code>的匹配相关度当然要提高权重比例了。</p>
<h2 id="3-8-bool-布尔查询"><a href="#3-8-bool-布尔查询" class="headerlink" title="3.8 bool - 布尔查询"></a>3.8 bool - 布尔查询</h2><p>可以组合多重查询</p>
<ul>
<li><p>must：查询必须匹配搜索条件，譬如 and</p>
</li>
<li><p>should：查询匹配满足1个以上条件，譬如 or</p>
</li>
<li><p>must_not：不匹配搜索条件，一个都不要满足</p>
</li>
<li><p>实操1：</p>
  <figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br></pre></td><td class="code"><pre><span class="line">POST /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;bool&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;must&quot;</span>: [</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="string">&quot;multi_match&quot;</span>: &#123;</span><br><span class="line">                        <span class="string">&quot;query&quot;</span>: <span class="string">&quot;慕课网&quot;</span>,</span><br><span class="line">                        <span class="string">&quot;fields&quot;</span>: [<span class="string">&quot;desc&quot;</span>, <span class="string">&quot;nickname&quot;</span>]</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="string">&quot;term&quot;</span>: &#123;</span><br><span class="line">                        <span class="string">&quot;sex&quot;</span>: <span class="number">1</span></span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="string">&quot;term&quot;</span>: &#123;</span><br><span class="line">                        <span class="string">&quot;birthday&quot;</span>: <span class="string">&quot;1996-01-14&quot;</span></span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            ]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">POST /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;bool&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;should（must_not）&quot;</span>: [</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="string">&quot;multi_match&quot;</span>: &#123;</span><br><span class="line">                        <span class="string">&quot;query&quot;</span>: <span class="string">&quot;学习&quot;</span>,</span><br><span class="line">                        <span class="string">&quot;fields&quot;</span>: [<span class="string">&quot;desc&quot;</span>, <span class="string">&quot;nickname&quot;</span>]</span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;,</span><br><span class="line">                &#123;</span><br><span class="line">                	<span class="string">&quot;match&quot;</span>: &#123;</span><br><span class="line">                		<span class="string">&quot;desc&quot;</span>: <span class="string">&quot;游戏&quot;</span></span><br><span class="line">                	&#125;	</span><br><span class="line">                &#125;,</span><br><span class="line">                &#123;</span><br><span class="line">                    <span class="string">&quot;term&quot;</span>: &#123;</span><br><span class="line">                        <span class="string">&quot;sex&quot;</span>: <span class="number">0</span></span><br><span class="line">                    &#125;</span><br><span class="line">                &#125;</span><br><span class="line">            ]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>实操2：</p>
  <figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">POST /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;bool&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;must&quot;</span>: [</span><br><span class="line">                &#123;</span><br><span class="line">                	<span class="string">&quot;match&quot;</span>: &#123;</span><br><span class="line">                		<span class="string">&quot;desc&quot;</span>: <span class="string">&quot;慕&quot;</span></span><br><span class="line">                	&#125;	</span><br><span class="line">                &#125;,</span><br><span class="line">                &#123;</span><br><span class="line">                	<span class="string">&quot;match&quot;</span>: &#123;</span><br><span class="line">                		<span class="string">&quot;nickname&quot;</span>: <span class="string">&quot;慕&quot;</span></span><br><span class="line">                	&#125;	</span><br><span class="line">                &#125;</span><br><span class="line">            ],</span><br><span class="line">            <span class="string">&quot;should&quot;</span>: [</span><br><span class="line">                &#123;</span><br><span class="line">                	<span class="string">&quot;match&quot;</span>: &#123;</span><br><span class="line">                		<span class="string">&quot;sex&quot;</span>: <span class="string">&quot;0&quot;</span></span><br><span class="line">                	&#125;	</span><br><span class="line">                &#125;</span><br><span class="line">            ],</span><br><span class="line">            <span class="string">&quot;must_not&quot;</span>: [</span><br><span class="line">                &#123;</span><br><span class="line">                	<span class="string">&quot;term&quot;</span>: &#123;</span><br><span class="line">                		<span class="string">&quot;birthday&quot;</span>: <span class="string">&quot;1992-12-24&quot;</span></span><br><span class="line">                	&#125;	</span><br><span class="line">                &#125;</span><br><span class="line">            ]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>Head 可视化组合查询</p>
<p>  <img src="https://gitee.com/littlefxc/oss/raw/master/images/es-12_3.jpg" alt="https://gitee.com/littlefxc/oss/raw/master/images/es-12_3.jpg"></p>
</li>
<li><p>为指定词语加权</p>
<p>  特殊场景下，某些词语可以单独加权，这样可以排得更加靠前。</p>
  <figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">POST /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;bool&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;should&quot;</span>: [</span><br><span class="line">            	&#123;</span><br><span class="line">            		<span class="string">&quot;match&quot;</span>: &#123;</span><br><span class="line">            			<span class="string">&quot;desc&quot;</span>: &#123;</span><br><span class="line">            				<span class="string">&quot;query&quot;</span>: <span class="string">&quot;律师&quot;</span>,</span><br><span class="line">            				<span class="string">&quot;boost&quot;</span>: <span class="number">18</span></span><br><span class="line">            			&#125;</span><br><span class="line">            		&#125;</span><br><span class="line">            	&#125;,</span><br><span class="line">            	&#123;</span><br><span class="line">            		<span class="string">&quot;match&quot;</span>: &#123;</span><br><span class="line">            			<span class="string">&quot;desc&quot;</span>: &#123;</span><br><span class="line">            				<span class="string">&quot;query&quot;</span>: <span class="string">&quot;进修&quot;</span>,</span><br><span class="line">            				<span class="string">&quot;boost&quot;</span>: <span class="number">2</span></span><br><span class="line">            			&#125;</span><br><span class="line">            		&#125;</span><br><span class="line">            	&#125;</span><br><span class="line">            ]</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<h2 id="3-9-post-filter-过滤器"><a href="#3-9-post-filter-过滤器" class="headerlink" title="3.9 post_filter - 过滤器"></a>3.9 post_filter - 过滤器</h2><p>对搜索出来的结果进行数据过滤。不会到es库里去搜，不会去计算文档的相关度分数，所以过滤的性能会比较高，过滤器可以和全文搜索结合在一起使用。<code>post_filter</code>元素是一个顶层元素，只会对搜索结果进行过滤。不会计算数据的匹配度相关性分数，不会根据分数去排序，query则相反，会计算分数，也会按照分数去排序。</p>
<p>使用场景：</p>
<ul>
<li>query：根据用户搜索条件检索匹配记录</li>
<li>post_filter：用于查询后，对结果数据的筛选</li>
</ul>
<p>实操：查询账户金额大于80元，小于160元的用户。并且生日在1998-07-14的用户</p>
<ul>
<li>gte：大于等于</li>
<li>lte：小于等于</li>
<li>gt：大于</li>
<li>lt：小于（除此以外还能做其他的match等操作也行）</li>
</ul>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">POST /shop/_doc/_search</span><br><span class="line"></span><br><span class="line">&#123;</span><br><span class="line">	<span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">		<span class="string">&quot;match&quot;</span>: &#123;</span><br><span class="line">			<span class="string">&quot;desc&quot;</span>: <span class="string">&quot;慕课网游戏&quot;</span></span><br><span class="line">		&#125;	</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&quot;post_filter&quot;</span>: &#123;</span><br><span class="line">		<span class="string">&quot;range&quot;</span>: &#123;</span><br><span class="line">			<span class="string">&quot;money&quot;</span>: &#123;</span><br><span class="line">				<span class="string">&quot;gt&quot;</span>: <span class="number">60</span>,</span><br><span class="line">				<span class="string">&quot;lt&quot;</span>: <span class="number">1000</span></span><br><span class="line">			&#125;</span><br><span class="line">		&#125;</span><br><span class="line">	&#125;	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="3-10-sort-排序"><a href="#3-10-sort-排序" class="headerlink" title="3.10 sort - 排序"></a>3.10 sort - 排序</h2><p>es的排序同sql，可以desc也可以asc。也支持组合排序。</p>
<ul>
<li><p>实操：</p>
  <figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">POST /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">	<span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">		<span class="string">&quot;match&quot;</span>: &#123;</span><br><span class="line">			<span class="string">&quot;desc&quot;</span>: <span class="string">&quot;慕课网游戏&quot;</span></span><br><span class="line">		&#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&quot;post_filter&quot;</span>: &#123;</span><br><span class="line">    	<span class="string">&quot;range&quot;</span>: &#123;</span><br><span class="line">    		<span class="string">&quot;money&quot;</span>: &#123;</span><br><span class="line">    			<span class="string">&quot;gt&quot;</span>: <span class="number">55.8</span>,</span><br><span class="line">    			<span class="string">&quot;lte&quot;</span>: <span class="number">155.8</span></span><br><span class="line">    		&#125;</span><br><span class="line">    	&#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&quot;sort&quot;</span>: [</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="string">&quot;age&quot;</span>: <span class="string">&quot;desc&quot;</span></span><br><span class="line">        &#125;,</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="string">&quot;money&quot;</span>: <span class="string">&quot;desc&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>对文本排序</p>
<p>  由于文本会被分词，所以往往要去做排序会报错，通常我们可以为这个字段增加额外的一个附属属性，类型为keyword，用于做排序。</p>
<ol>
<li><p>创建新的索引</p>
 <figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line">POST /shop2/_mapping</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;properties&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;id&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;type&quot;</span>: <span class="string">&quot;long&quot;</span></span><br><span class="line">        &#125;,</span><br><span class="line">        <span class="string">&quot;nickname&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;type&quot;</span>: <span class="string">&quot;text&quot;</span>,</span><br><span class="line">            <span class="string">&quot;analyzer&quot;</span>: <span class="string">&quot;ik_max_word&quot;</span>,</span><br><span class="line">            <span class="string">&quot;fields&quot;</span>: &#123;</span><br><span class="line">                <span class="string">&quot;keyword&quot;</span>: &#123;</span><br><span class="line">                    <span class="string">&quot;type&quot;</span>: <span class="string">&quot;keyword&quot;</span></span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>插入数据</p>
 <figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">POST /shop2/_doc</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;id&quot;</span>: <span class="number">1001</span>,</span><br><span class="line">    <span class="string">&quot;nickname&quot;</span>: <span class="string">&quot;美丽的风景&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">POST /shop2/_doc</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;id&quot;</span>: <span class="number">1002</span>,</span><br><span class="line">    <span class="string">&quot;nickname&quot;</span>: <span class="string">&quot;漂亮的小哥哥&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">POST /shop2/_doc</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;id&quot;</span>: <span class="number">1003</span>,</span><br><span class="line">    <span class="string">&quot;nickname&quot;</span>: <span class="string">&quot;飞翔的巨鹰&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">POST /shop2/_doc</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;id&quot;</span>: <span class="number">1004</span>,</span><br><span class="line">    <span class="string">&quot;nickname&quot;</span>: <span class="string">&quot;完美的天空&quot;</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">POST /shop2/_doc</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;id&quot;</span>: <span class="number">1005</span>,</span><br><span class="line">    <span class="string">&quot;nickname&quot;</span>: <span class="string">&quot;广阔的海域&quot;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>排序</p>
 <figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">POST /shop2/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;sort&quot;</span>: [</span><br><span class="line">        &#123;</span><br><span class="line">            <span class="string">&quot;nickname.keyword&quot;</span>: <span class="string">&quot;desc&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ol>
</li>
</ul>
<h2 id="3-11-highlight-高亮显示"><a href="#3-11-highlight-高亮显示" class="headerlink" title="3.11 highlight - 高亮显示"></a>3.11 highlight - 高亮显示</h2><figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">POST /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;match&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;desc&quot;</span>: <span class="string">&quot;慕课网&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="string">&quot;highlight&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;pre_tags&quot;</span>: [<span class="string">&quot;&lt;span&gt;&quot;</span>],</span><br><span class="line">        <span class="string">&quot;post_tags&quot;</span>: [<span class="string">&quot;&lt;/span&gt;&quot;</span>],</span><br><span class="line">        <span class="string">&quot;fields&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;desc&quot;</span>: &#123;&#125;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="3-12-prefix-前缀"><a href="#3-12-prefix-前缀" class="headerlink" title="3.12 prefix - 前缀"></a>3.12 prefix - 前缀</h2><p>根据前缀去查询</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">POST /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">    <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">        <span class="string">&quot;prefix&quot;</span>: &#123;</span><br><span class="line">            <span class="string">&quot;desc&quot;</span>: <span class="string">&quot;imo&quot;</span></span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;                                                                  </span><br></pre></td></tr></table></figure>

<h2 id="3-13-fuzzy-模糊搜索"><a href="#3-13-fuzzy-模糊搜索" class="headerlink" title="3.13 fuzzy - 模糊搜索"></a>3.13 fuzzy - 模糊搜索</h2><p>模糊搜索，并不是指的sql的模糊搜索，而是用户在进行搜索的时候的打字错误现象，搜索引擎会自动纠正，然后尝试匹配索引库中的数据。</p>
<figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line">POST /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;fuzzy&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;desc&quot;</span>: <span class="string">&quot;imoov.coom&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"># 或多字段搜索</span><br><span class="line">POST /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;multi_match&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;fields&quot;</span>: [ <span class="string">&quot;desc&quot;</span>, <span class="string">&quot;nickname&quot;</span>],</span><br><span class="line">      <span class="string">&quot;query&quot;</span>: <span class="string">&quot;imcoc supor&quot;</span>,</span><br><span class="line">      <span class="string">&quot;fuzziness&quot;</span>: <span class="string">&quot;AUTO&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">POST /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;multi_match&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;fields&quot;</span>: [ <span class="string">&quot;desc&quot;</span>, <span class="string">&quot;nickname&quot;</span>],</span><br><span class="line">      <span class="string">&quot;query&quot;</span>: <span class="string">&quot;演说&quot;</span>,</span><br><span class="line">      <span class="string">&quot;fuzziness&quot;</span>: <span class="string">&quot;1&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>官文：<a target="_blank" rel="noopener" href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/fuzzy-match-query.html">https://www.elastic.co/guide/cn/elasticsearch/guide/current/fuzzy-match-query.html</a></li>
</ul>
<h2 id="3-14-wildcard-占位符查询"><a href="#3-14-wildcard-占位符查询" class="headerlink" title="3.14 wildcard - 占位符查询"></a>3.14 wildcard - 占位符查询</h2><p>占位符查询。</p>
<ul>
<li><p>？：1个字符</p>
</li>
<li><p>：1个或多个字符</p>
  <figure class="highlight jsx"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">POST /shop/_doc/_search</span><br><span class="line">&#123;</span><br><span class="line">  <span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">    <span class="string">&quot;wildcard&quot;</span>: &#123;</span><br><span class="line">      <span class="string">&quot;desc&quot;</span>: <span class="string">&quot;*oo?&quot;</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br><span class="line">&#123;</span><br><span class="line">	<span class="string">&quot;query&quot;</span>: &#123;</span><br><span class="line">    	<span class="string">&quot;wildcard&quot;</span>: &#123;</span><br><span class="line">    		<span class="string">&quot;desc&quot;</span>: <span class="string">&quot;演*&quot;</span></span><br><span class="line">    	&#125;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
<li><p>官文：<a target="_blank" rel="noopener" href="https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html">https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html</a></p>
</li>
</ul>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/blog/tags/es/" rel="tag"># es</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/blog/2021/01/11/ES-%E4%B9%8B%E5%88%86%E8%AF%8D%E4%B8%8E%E5%86%85%E7%BD%AE%E5%88%86%E8%AF%8D%E5%99%A8/" rel="prev" title="ES 之分词与内置分词器">
                  <i class="fa fa-chevron-left"></i> ES 之分词与内置分词器
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/blog/2021/01/12/ES%20%E6%B7%B1%E5%BA%A6%E5%88%86%E9%A1%B5-%E6%BB%9A%E5%8A%A8%E6%90%9C%E7%B4%A2/" rel="next" title="ES 深度分页 & 滚动搜索">
                  ES 深度分页 & 滚动搜索 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">一年春又来</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/blog/js/utils.js"></script><script src="/blog/js/motion.js"></script><script src="/blog/js/schemes/muse.js"></script><script src="/blog/js/next-boot.js"></script>

  
<script src="/blog/js/local-search.js"></script>






  





</body>
</html>
